package com.tianbing.adlottery.controller.websiteadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.WebsiteBusiness;
import com.jubao.ejb.dao.WebsiteDao;
import com.jubao.ejb.entity.Website;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditWebsiteProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditWebsiteProfileController.class.getName());


	private WebsiteDao websiteDao;
	private WebsiteBusiness websiteBusiness;

	private SkinUtils skinUtils;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		Website website = websiteDao.find(wbsid);
		fb.setWebsite(website);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView()+"?wbsid="+wbsid);
		ControllerFormBean form = (ControllerFormBean) cmd;

		Website website = form.getWebsite();
		websiteBusiness.saveWebSiteProfile(website);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		EditWebsiteProfileController.logger = logger;
	}


	/**
	 * @param websiteDao the websiteDao to set
	 */
	public void setWebsiteDao(WebsiteDao websiteDao) {
		this.websiteDao = websiteDao;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setWebsiteBusiness(WebsiteBusiness websiteBusiness) {
		this.websiteBusiness = websiteBusiness;
	}

}
